//
//  HMSelectCheapViewController.m
//  HMMarket
//
//  Created by haohao on 2017/6/15.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "HMSelectCheapViewController.h"
#import "HMCheapTicketTableViewCell.h"
#import "HMNOSelectCheapTableViewCell.h"
@interface HMSelectCheapViewController ()<UITableViewDelegate, UITableViewDataSource, HMNOSelectCheapTableViewCellDelegate>

NS_STRONG NSMutableArray * couponArray;
NS_STRONG UITableView *myTableView;

@end

@implementation HMSelectCheapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的优惠劵";
    [self setupTableView];
    [self setupData];
}

- (void)setupTableView
{
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, KWidthSCREEN, KHeightSCREEN) style:UITableViewStyleGrouped];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.backgroundColor = kBackGolor;
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
}
- (void)setupData
{
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    [param setObjOrNil:[MGAccountTool getUid] forKey:@"uid"];
    [param setObjOrNil:@(_totalPrice) forKey:@"order_price"];

    [HttpBaseTool get:kCouponList params:param success:^(id responseObj) {
        CouponModel * model = [CouponModel mj_objectWithKeyValues:responseObj[@"data"]];
        self.couponArray = model.list;
        if (self.couponArray.count == 0) {
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, KWidthSCREEN, 30)];
            titleLabel.textAlignment = NSTextAlignmentCenter;
            titleLabel.font = [UIFont systemFontOfSize:14.0f];
            titleLabel.text = @"无可用优惠券";
            self.myTableView.tableFooterView = titleLabel;
        } else {
            [self.myTableView reloadData];
        }
    } failure:^(NSError *error) {
        if (error.code != NSURLErrorCancelled) {
            [HMHUDManager showHUDWithText:NetWorkError];
        }
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return self.couponArray.count;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HMNOSelectCheapTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"HMNOSelectCheapTableViewCell" owner:self options:nil] firstObject];
        cell.delegate = self;
        if (__detail != nil) {
            cell.notUser = 0;
        } else {
            cell.notUser = 1;
        }
        return cell;
    } else {
        HMCheapTicketTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"HMCheapTicketTableViewCell" owner:self options:nil] firstObject];
        cell.detail = self.couponArray[indexPath.row];
        return cell;
    }
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 44;
    } else {
        return 118;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        CouponDetail *detail = self.couponArray[indexPath.row];
        
        if ([detail.status isEqualToString:@"2"]) {
            
        } else {
            if (_getCheapTicket) {
                _getCheapTicket(detail);
                [self.navigationController popViewControllerAnimated:YES];
            }
        }
    }
 
}

#pragma mark - HMNOSelectCheapTableViewCellDelegate
- (void)sendSelectType:(BOOL)select {
    if (_getCheapTicket) {
        _getCheapTicket(nil);
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (NSMutableArray *)couponArray {
    if (_couponArray == nil) {
        _couponArray = [NSMutableArray array];
    }
    return _couponArray;
}

@end
